package cn.graydove.bot.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.UUID;

public class ShaUtil {
    private ShaUtil() {

    }

    public static String SHA256(String str) {

        MessageDigest messageDigest;

        String encodeStr = "";

        try {

            messageDigest = MessageDigest.getInstance("SHA-256");

            messageDigest.update(str.getBytes(StandardCharsets.UTF_8));

            encodeStr = byte2Hex(messageDigest.digest());

        } catch (Exception e) {

            System.out.println("getSHA256 is error" + e.getMessage());

        }

        return encodeStr;

    }


    public static String UUID() {

        return UUID.randomUUID().toString().replaceAll("-", "");

    }

    private static String byte2Hex(byte[] bytes) {

        StringBuilder builder = new StringBuilder();

        String temp;

        for (byte aByte : bytes) {

            temp = Integer.toHexString(aByte & 0xFF);

            if (temp.length() == 1) {

                builder.append("0");

            }

            builder.append(temp);

        }

        return builder.toString();

    }
}
